package com.wss.module.wan.ui.main.adapter;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.utils.TextUtils;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.Article;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;
import java.util.List;

/**
 * Describe：文章列表适配器
 * Created by 吴天强 on 2018/10/23.
 */
public class ArticleAdapter extends BaseListAdapter<Article> {

    private OnTagClickListener listener;

    public void setOnTagListener(OnTagClickListener listener) {
        this.listener = listener;
    }

    /**
     * 常规列表重写该方法
     *
     * @param context     context
     * @param list        数据源
     * @param listener    Item点击回调
     */
    public ArticleAdapter(Context context, List<Article> list, OnListItemClickListener<Article> listener) {
        super(context,
                list,
                ResourceTable.Layout_wan_item_of_article_list,
                listener);
    }

    @Override
    public void onBindData(@NotNull SuperViewHolder holder, int layoutPosition, @NotNull Article data) {
        holder.setText(ResourceTable.Id_tv_title, data.getTitle());
        holder.setText(ResourceTable.Id_tv_author, String.format("作者：%s", data.getAuthor()));
        holder.setText(ResourceTable.Id_tv_date, String.format("时间：%s", data.getNiceDate()));
        if (!TextUtils.isEmpty(data.getChapterName())) {
            holder.setText(ResourceTable.Id_tv_type1, data.getChapterName());
        }
        if (!TextUtils.isEmpty(data.getSuperChapterName())) {
            holder.setText(ResourceTable.Id_tv_type2, data.getSuperChapterName());
        }
        holder.findViewById(ResourceTable.Id_iv_collection).setClickedListener(v -> {
            if (listener != null) {
                listener.onCollectionClick(layoutPosition);
            }
        });
    }

    /**
     * Item 标签被点击
     */
    public interface OnTagClickListener {

        /**
         * 收藏按钮被点击
         *
         * @param position pos
         */
        void onCollectionClick(int position);
    }
}
